package con.shake;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.net.wifi.WifiManager;

public class Utils {
	public static final boolean getMobleDataEnabled(Context context)
			throws Exception {

		final String GET_MOBILE_DATA_ENABLED = "getMobileDataEnabled";
		final ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		final Class connectivityManager = Class
				.forName(cm.getClass().getName());

		final Method[] methods = connectivityManager.getDeclaredMethods();

		for (final Method method : methods) {
			if (GET_MOBILE_DATA_ENABLED.equals(method.getName())) {
				method.setAccessible(true);
				return (Boolean) method.invoke(cm);
			}
		}
		throw new Exception("Method not found");
	}

	public static final void setMobleDataEnabled(Context context,
			boolean enabled) throws Exception {
		final ConnectivityManager conman = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		final Class conmanClass = Class.forName(conman.getClass().getName());
		final Field iConnectivityManagerField = conmanClass
				.getDeclaredField("mService");
		iConnectivityManagerField.setAccessible(true);
		final Object iConnectivityManager = iConnectivityManagerField
				.get(conman);
		final Class iConnectivityManagerClass = Class
				.forName(iConnectivityManager.getClass().getName());
		final Method setMobileDataEnabledMethod = iConnectivityManagerClass
				.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
		setMobileDataEnabledMethod.setAccessible(true);
		setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
	}

	// private static final void setMobleDataEnabled(Context context,boolean
	// enabled) throws Exception{
	//
	// final String SET_MOBILE_DATA_ENABLED = "setMobileDataEnabled";
	// final ConnectivityManager cm = (ConnectivityManager)context
	// .getSystemService(Context.CONNECTIVITY_SERVICE);
	//
	// final Class connectivityManager = Class.forName(cm.getClass()
	// .getName());
	//
	// final Method[] methods = connectivityManager.getDeclaredMethods();
	//
	// for (final Method method : methods) {
	// Log.i("DREG", "NAME="+method.getName());
	// if(SET_MOBILE_DATA_ENABLED.equals(method.getName().trim())){
	// method.setAccessible(true);
	// method.invoke(cm,enabled);
	// }
	// }
	// throw new Exception("Method not found");
	// }
	// private void toggle3G(Context context){
	// try {
	//
	// final String SET_MOBILE_DATA_ENABLED = "setMobileDataEnabled";
	// final String GET_MOBILE_DATA_ENABLED = "getMobileDataEnabled";
	//
	// final ConnectivityManager cm = (ConnectivityManager)context
	// .getSystemService(Context.CONNECTIVITY_SERVICE);
	//
	// final Class connectivityManager = Class.forName(cm.getClass()
	// .getName());
	//
	// final Method[] methods = connectivityManager.getDeclaredMethods();
	//
	// for (final Method method : methods) {
	// Log.i("DREG", "NAME="+method.getName());
	//
	// if(GET_MOBILE_DATA_ENABLED.equals(method.getName())){
	// method.setAccessible(true);
	// Log.i("DREG", "VAL="+method.invoke(cm));
	// }
	//
	// // if (SET_MOBILE_DATA_ENABLED
	// // .equals(method.getName())) {
	// // method.invoke(cm, true);
	// // }
	// }
	//
	// } catch (final Exception e) {
	// e.printStackTrace();
	// }
	// }

	public static final boolean getWifiEnabled(Context context)
			throws Exception {
		return ((WifiManager)context.getSystemService(Context.WIFI_SERVICE)).isWifiEnabled();
	}

	public static final void setWifiEnabled(Context context, boolean enabled)
			throws Exception {
		((WifiManager)context.getSystemService(Context.WIFI_SERVICE)).setWifiEnabled(enabled);
	}

	private static MediaPlayer player;

	public static void startSound(Context context, Uri uri) {
		try {
			if (player == null) {
				createMP();
			}

			player.reset();
			player.setDataSource(context, uri);
			player.prepareAsync();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void createMP() {
		if (player != null) {
			player.release();
			player = null;
		}

		player = new MediaPlayer();
		player.setAudioStreamType(AudioManager.STREAM_MUSIC);
		// player.setVolume(5.0f, 5.0f);
		player.setOnPreparedListener(new OnPreparedListener() {

			@Override
			public void onPrepared(MediaPlayer mp) {
				// TODO Auto-generated method stub
				// mp.seekTo(0);
				mp.setVolume(5.0f, 5.0f);
				mp.start();
			}

		});
		player.setOnErrorListener(new OnErrorListener() {

			@Override
			public boolean onError(MediaPlayer mp, int what, int extra) {
				// TODO Auto-generated method stub
				mp.reset();
				return false;
			}

		});
		player.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub
			}

		});
	}
	
	public static boolean isServiceRunning(Context context,
			Class serviceClassName) {

		final ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		final List<RunningServiceInfo> services = activityManager
				.getRunningServices(Integer.MAX_VALUE);

		for (RunningServiceInfo runningServiceInfo : services) {
			if (runningServiceInfo.service.getClassName().equals(
					serviceClassName.getCanonicalName())) {
				return true;
			}
		}
		return false;
	}
}
